<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <my-search @click.native="gotoSearch"></my-search>
    </view>

    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`"
          :open-type="item.open_type">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类，秒杀，超市，母婴 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item,index) in navList" :key="index" @click="skipToCate(index)">
        <image :src="item.image_src" mode="widthFix"></image>
      </view>
    </view>

    <!-- 显示广告栏 -->
    <uni-notice-bar speed="50" show-icon scrollable :showClose="true"
      text="uni-app 版正式发布，开发一次，同时发布iOS、Android、H5、微信小程序、支付宝小程序、百度小程序、头条小程序等7大平台。" />

    <!-- 楼层 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item,index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <view class="con">
          <!-- 楼层图片展示 -->
          <view class="floor-left">
            <image :src="item.product_list[0].image_src"></image>
          </view>
          <view class="floor-right">
            <navigator :url="ele.url" class="floor-right-item" v-for="(ele,index) in item.product_list" :key="index"
              v-if="index !== 0">
              <image :src="ele.image_src"></image>
            </navigator>
          </view>
        </view>
      </view>

    </view>

  </view>
</template>

<script>
  import {
    $http
  } from '@escook/request-miniprogram';
  export default {
    data() {
      return {
        //轮播图数组
        swiperList: [],
        //分类导航数据
        navList: {},
        //楼层的数据
        floorList: {},
      };
    },
    //页面生命周期函数 —— 加载页面时
    onLoad() {
      //调用函数，获取轮播图数据
      this.getSwiperList()
      //调用函数，获取分类导航数据
      this.getNavList()
      //调用函数，获取楼层数据
      this.getFloorList()
    },
    methods: {
      //获取轮播图数据
      async getSwiperList() {
        let res = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.statusCode === 200) {
          this.swiperList = res.data.message
        } else return uni.$showMsg()
      },

      //获取分类导航的数据
      async getNavList() {
        let {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status === 200) {
          this.navList = res.message
        } else return uni.$showMsg()
      },

      //点击分类切换到分类tabBar页面
      skipToCate(index) {
        if (index === 0) {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },

      //获取首页楼层的数据
      async getFloorList() {
        let {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status === 200) {
          res.message.map(floor => {
            floor.product_list.map(item => {
              item.url = '/subpkg/goods_list/goods_list?' + item.navigator_url.split('?')[1]
            })
          })
          this.floorList = res.message
        } else return uni.$showMsg()
      },

      //点击搜索后，跳转到搜索页面
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },
    }
  }
</script>

<style lang="scss">
  .swiper-item {
    height: 330rpx;
  }

  .swiper-item image {
    width: 100%;
    height: 100%;
  }

  .nav-list {
    width: 100%;
    height: 150rpx;
    margin: 15rpx 0rpx;
    display: flex;
    justify-content: space-around;
  }

  .nav-list .nav-item image {
    width: 128rpx;
    height: 140rpx;
  }

  .floor-list {
    margin-top: 30rpx;
  }

  .floor-item {
    margin: 30rpx 0rpx;

  }

  .floor-title {
    height: 60rpx;
    width: 100%;
    display: flex;
  }

  .con {
    display: flex;
  }

  .floor-left {
    height: 280rpx;
    width: 30%;
    margin-right: 10rpx;
  }

  .floor-left image {
    height: 100%;
    width: 100%;
  }

  .floor-right {
    display: flex;
    width: 70%;
    height: 300rpx;
    //允许换行
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .floor-right-item {
    width: 250rpx;
    height: 130rpx;
  }

  .floor-right-item image {
    width: 100%;
    height: 100%;
  }

  .search-box {
    //固定不能
    position: sticky;
    //必须加这个
    top: 0;
    //提高层级，防止在滚动的过程中被覆盖
    z-index: 999;
  }
</style>
